package co.fitstart.admin.entity.search;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.persistence.support.Filter;
import com.demo2do.core.persistence.support.Order;
import com.demo2do.core.persistence.support.QueryBuilder;

import co.fitstart.entity.principal.PrincipalType;
import co.fitstart.entity.site.Site;

public class PrincipalSearch implements QueryBuilder {
    
    private List<Site> sites;
    
    private String name;
    
    private String authority;
    
    private Boolean disabled;
    
    private Filter filter;
    
    public PrincipalSearch() {
        
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public Filter getPreparedFilter(boolean refresh) {
        if(filter == null || refresh) {
            if(sites == null) {
                filter =  new Filter().addEquals("type", PrincipalType.SITE)
                                      .addEquals("disabled", disabled)
                                      .addLike("name", name)
                                      .addEquals("authority", authority);
            } else {
                if("ROLE_AREA_MANAGER".equals(authority)) {
                    filter =  new Filter().addEquals("site.areaManager.type", PrincipalType.SITE)
                                          .addEquals("site.areaManager.disabled", disabled)
                                          .addLike("site.areaManager.name", name)
                                          .addEquals("site.areaManager.authority", authority)
                                          .addSentence("site IN (:sites)", "sites", sites);
                } else if("ROLE_AREA_SERVER".equals(authority)) {
                    filter =  new Filter().addEquals("site.areaServer.type", PrincipalType.SITE)
                                          .addEquals("site.areaServer.disabled", disabled)
                                          .addLike("site.areaServer.name", name)
                                          .addEquals("site.areaServer.authority", authority)
                                          .addSentence("site IN (:sites)", "sites", sites);
                } else if("ROLE_SITE_MANAGER".equals(authority)) {
                    filter =  new Filter().addIsNull("relation.coach")
                                          .addEquals("relation.manager.type", PrincipalType.SITE)
                                          .addEquals("relation.manager.disabled", disabled)
                                          .addLike("relation.manager.name", name)
                                          .addEquals("relation.manager.authority", authority)
                                          .addSentence("relation.site IN (:sites)", "sites", sites);
                } else {
                    filter =  new Filter().addIsNotNull("relation.coach")
                                          .addEquals("relation.coach.type", PrincipalType.SITE)
                                          .addEquals("relation.coach.disabled", disabled)
                                          .addLike("relation.coach.name", name)
                                          .addEquals("relation.coach.authority", authority)
                                          .addSentence("relation.site IN (:sites)", "sites", sites);
                }
            }
        }
        
        return filter;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public Order getOrder() {
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        if(sites == null) {
            return "FROM Principal " + getPreparedFilter(false).getWhereSentence();
        } else if("ROLE_AREA_MANAGER".equals(authority)) {
            return "SELECT site.areaManager FROM Site site " + getPreparedFilter(false).getWhereSentence();
        } else if("ROLE_AREA_SERVER".equals(authority)) {
            return "SELECT site.areaServer FROM Site site " + getPreparedFilter(false).getWhereSentence();
        } else if("ROLE_SITE_MANAGER".equals(authority)) {
            return "SELECT relation.manager FROM PrincipalReportRelation relation" + getPreparedFilter(false).getWhereSentence();
        } else {
            return "SELECT relation.coach FROM PrincipalReportRelation relation " + getPreparedFilter(false).getWhereSentence();
        }
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).getParameters();
    }

    /**
     * @return the sites
     */
    public List<Site> getSites() {
        return sites;
    }

    /**
     * @return the name
     */
    public String getName() {
        return StringUtils.isBlank(name) ? null : name;
    }

    /**
     * @return the authority
     */
    public String getAuthority() {
        return StringUtils.isBlank(authority) ? null : authority;
    }

    /**
     * @return the disabled
     */
    public Boolean getDisabled() {
        return disabled;
    }

    /**
     * @param sites the sites to set
     */
    public void setSites(List<Site> sites) {
        this.sites = sites;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param authority the authority to set
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }
}
